﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MayhemWpf.UserControls;
using OL2012Mayhem.Helpers;
using OL2012Mayhem.Events;

namespace OL2012Mayhem.Wpf
{
    /// <summary>
    /// Interaction logic for OL2012MedalsConfig.xaml
    /// </summary>
    public partial class OL2012MedalsConfig : WpfConfiguration
    {

        private List<CountryEntry> countries;

        public OL2012MedalsConfig(string country, string medalType)
        {
            InitializeComponent();
            
            countries = CollectDataHelper.GetCountries();

            foreach (var item in countries)
            {
                this.CountryComboBox.Items.Add(item.Country.ToString());
            }
            this.CountryComboBox.SelectedIndex = this.CountryComboBox.Items.IndexOf(country);

            this.MedalTypeComboBox.Items.Add(MedalTypes.AllMedals);
            this.MedalTypeComboBox.Items.Add(MedalTypes.GoldMedals);
            this.MedalTypeComboBox.Items.Add(MedalTypes.SilverMedals);
            this.MedalTypeComboBox.Items.Add(MedalTypes.BronzeMedals);

            this.MedalTypeComboBox.SelectedIndex = this.MedalTypeComboBox.Items.IndexOf(medalType);

            Country = country;
            MedalType = medalType;

        }

        public override string Title
        {
            get
            {
                return "OL 2012 Medals";
            }
        }

        public string Country
        {
            get;
            set;
        }

        public string MedalType
        {
            get;
            set;
        }

        public string Url
        {
            get;
            private set;
        }

        public override void OnLoad()
        {
            this.CountryComboBox.SelectedIndex = this.CountryComboBox.Items.IndexOf(Country);
            this.MedalTypeComboBox.SelectedIndex = this.MedalTypeComboBox.Items.IndexOf(MedalType);
        }

        public override void OnSave()
        {
            string selectedCountry = CountryComboBox.SelectedValue.ToString();
            string selectedMedalType = MedalTypeComboBox.SelectedValue.ToString();
            if (!string.IsNullOrEmpty(selectedCountry))
            {
                Country = selectedCountry;
                MedalType = selectedMedalType;

                string countryUrl = (from c in countries where c.Country.Equals(selectedCountry) select c).FirstOrDefault().Url;
                Url = countryUrl;
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CanSave = true;
            if (string.IsNullOrEmpty(CountryComboBox.SelectedValue.ToString()) || string.IsNullOrEmpty((string)MedalTypeComboBox.SelectedValue))
            {
                CanSave = false;
            }
        }
    }
}
